<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: Iurii_Miedviediev
  Date: 9/18/2014
  Time: 12:32 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>

    <!-- Custom CSS -->
    <link href="/css/orders.css" rel="stylesheet">

    <%@ include file="../base/headconfig.jsp" %>

    <script src="/js/jquery-1.11.0.js" type="text/javascript"></script>
    <title>Admin Panel</title>
</head>
<body>

<script type="text/javascript">
    $(document).ready(
            function(){
                $('.ordertab').hide();
            });
</script>

<%@ include file="../base/header.jsp" %>

<!-- Content -->
<div class="intro-header">

    <div class="container">

        <div class="intro-message">

            <div class="btn-group">
                <a href="#" onclick="if ($('.usertab').css('display')=='none'){$('.usertab, .ordertab').toggle();};">
                    <button type="button" class="btn btn-default">
                        Users
                    </button>
                </a>
                <a href="#" onclick="if ($('.ordertab').css('display')=='none'){$('.usertab, .ordertab').toggle();};">
                    <button type="button" class="btn btn-default">
                        Orders
                    </button>
                </a>
            </div>

            <div id="Content">
                <div class="usertab">
                    <table style="width: 800px">
                        <thead>
                        <tr>
                            <th>User ID</th>
                            <th>Login</th>
                            <th>Name</th>
                            <th>Surname</th>
                            <th>Email</th>
                            <th>Active Orders</th>
                            <th>Role</th>
                            <th>Discount</th>
                            <th>Remove</th>
                        </tr>
                        </thead>
                        <c:forEach var="user" items="${users}">
                            <tr>
                                <td align="center"><strong style="font-size: 16;">#${user.id}</strong></td>
                                <td>${user.login}</td>
                                <td>${user.firstName}</td>
                                <td>${user.lastName}</td>
                                <td>${user.email}</td>
                                <td>${fn:length(user.orders)}</td>
                                <td>
                                    <form style="margin-top: 10px" method="post" action="/admin/user?role">
                                        <input type="hidden" name="id" value="${user.id}">
                                        <select name="newRole">
                                            <c:choose>
                                                <c:when test="${user.role == 'ROLE_USER'}">
                                                    <option selected value="ROLE_USER">Simple User</option>
                                                    <option value="ROLE_ADMIN">Administrator</option>
                                                </c:when>
                                                <c:otherwise>
                                                    <option value="ROLE_USER">Simple User</option>
                                                    <option selected value="ROLE_ADMIN">Administrator</option>
                                                </c:otherwise>
                                            </c:choose>

                                        </select>
                                        <input type="submit" value="OK">
                                    </form>
                                </td>
                                <td>
                                    <form style="margin-top: 10px" method="post" action="/admin/user?discount">
                                        <input type="hidden" name="id" value="${user.id}">
                                        <input style="width: 40px" type="text" name="newDiscount"
                                               placeholder="<fmt:parseNumber integerOnly="true"
                                               type="number" value="${user.discount*100}" />%">
                                        <input type="submit" value="OK">
                                    </form>
                                </td>
                                <td align="center"><a href="/admin/user?remove&id=${user.id}"><span class="glyphicon glyphicon-remove"></span></a></td>
                            </tr>
                        </c:forEach>
                    </table>
                </div>
                <div class="ordertab">
                    <table>
                        <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>User</th>
                            <th>Books</th>
                            <th>Date</th>
                            <th>Price</th>
                            <th>Status</th>
                        </tr>
                        </thead>
                        <c:forEach var="order" items="${orders}">
                            <tr>
                                <c:set var="books" value="${order.items}"/>

                                <!-- Finding total price of the order -->
                                <c:set var="sum" value="${0}"/>
                                <c:forEach var="book" items="${books}">
                                    <c:set var="sum" value="${sum + book.price}"/>
                                </c:forEach>

                                <td align="center"><a href="/order/${order.id}"><strong style="font-size: 16;">#${order.id}</strong></a></td>
                                <td>${order.user.login}</td>
                                <td>${fn:length(books)}</td>
                                <td><fmt:formatDate value="${order.dateOfCreation}" pattern="dd/MM/yyyy - HH:mm"/></td>
                                <td>$${sum/100}</td>
                                <td><span class="orderstatus">${order.status}</span></td>
                            </tr>
                        </c:forEach>
                    </table>
                </div>
            </div>

        </div>

    </div>
    <!-- /.container -->
</div>
<!-- /.Content -->

<%@ include file="../base/footer.jsp" %>
</body>
</html>

